﻿using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

public static class SelectObjectController<S, T> where S : StateDataBase, new() where T : StateTransitionDataBase, new()
{

    private static object selectObject;

    public static object SelectObject
    {
        get
        {
            return selectObject;
        }
        set
        {
            selectObject = value;
        }
    }

    /// <summary>
    /// 复制状态数据 S 
    /// </summary>
    public static S CopyStateData
    {
        get
        {
            return copyStateData;
        }

        set
        {
            copyStateData = value;
        }
    }

    private static S copyStateData;

    public static bool IsSelectThis(object item)
    {
        if (selectObject == null)
            return false;
        return item.Equals(selectObject);
    }

    public static void SelectItemObject(object item)
    {
        if (item != selectObject)
        {
            //去掉文本输入的焦点
            EditorGUI.FocusTextInControl("");           
        }
        SelectObjectCancel();
        selectObject = item;
        if (selectObject is StateGUIData<S>)
        {
            ((StateGUIData<S>)selectObject).uiState = MachineStateUIStateEnum.select;
        }
        else if (selectObject is TransitionGUIData<T>)
        {
            ((TransitionGUIData<T>)selectObject).uiState = MachineStateUIStateEnum.select;

        }
    }
    public static void SelectObjectCancel()
    {
        if (selectObject != null)
        {
            if (selectObject is StateGUIData<S>)
            {
                ((StateGUIData<S>)selectObject).uiState = MachineStateUIStateEnum.Normal;
            }
            else if (selectObject is TransitionGUIData<T>)
            {
                ((TransitionGUIData<T>)selectObject).uiState = MachineStateUIStateEnum.Normal;
            }
        }
        selectObject = null;
    }

    public static void DeleteSelectObjet()
    {
        if (selectObject != null)
        {
            if (selectObject is StateGUIData<S>)
            {
                SM_EditorController<S, T>.Instance.machineData.DeleteState(((StateGUIData<S>)selectObject).stateData.id);

            }
            else if (selectObject is TransitionGUIData<T>)
            {
                SM_EditorController<S, T>.Instance.machineData.DeleteTransitions(((TransitionGUIData<T>)selectObject).transitions);
                //Debug.Log("DeleteSelectObjet");
            }
        }
    }
}

